<template>
    <layout title="产品需求信息" :showTabBar="false">
        <x-icon slot="icon" @click="onAdd" type="plus" style="fill:#fff;position:relative;top:-4px;right:3px;"></x-icon>

        <group v-for="li in list">
            <cell :value="li.total" :border-intent="false" arrow-direction="right">
                <div slot="title">
                    {{ li.created_at | time_bbs }} 报价
                </div>
            </cell>
            <template>
                <cell-form-preview :border-intent="false" :list="li.info"></cell-form-preview>
            </template>
        </group>
    </layout>

</template>

<script>
    import { XInput, Group, Cell, Divider, PopupPicker, XButton, CellFormPreview } from 'vux'
    import Layout from './Layout.vue'
    import { batch as api } from '../api'
    import { mapGetters } from 'vuex'
    import _ from 'lodash'

    export default {
        name: 'batch',
        components: {
            XInput,
            Group,
            Cell,
            Divider,
            PopupPicker,
            Layout,
            XButton,
            CellFormPreview,
        },
        data() {
            return {
                customer_id: null,
                list: []
            }
        },
        computed: {
            ...mapGetters(['dicObj'])
        },

        methods: {
            onAdd() {
                this.$router.push({ path: '/batch-edit', query: this.$route.query })
            },
            async getList() {
                let ret = await api.index({
                    customer_id: this.customer_id
                })
                let list = []
                for (let item of ret.data) {
                    let total = 0
                    let obj = {
                        id: item.id,
                        created_at: item.created_at,
                        info: _.map(item.batch_products, t => {
                            var value = t.num * t.sale_price;
                            total += value;
                            return { label: `${t.name} x ${t.num}`, value }
                        }),
                        total,
                    }
                    list.push(obj)
                }
                this.list = list
            },
        },
        async mounted() {
            this.$store.commit('showBack', true)
            this.$store.commit('selectedTab', 'sale')
            this.customer_id = this.$route.query.customer_id
            await this.getList()
        },
    }

</script>
